package com.xzlf;

import lombok.Data;

import java.awt.*;
import java.util.Random;
import java.util.UUID;

@Data
public class Tank {
    int x = 200, y = 200;

    TankFrame frame;
    private static final int SPEED = 10;
    private Dir dir = Dir.UP;
    private UUID id = null;
    public static final int WIDTH = ResourceMgr.tankD.getWidth();
    public static final int HEIGHT = ResourceMgr.tankD.getHeight();
    private boolean moving = true;

    private Random random = new Random();
    private Group group = Group.BAD;

    Rectangle rectangle;

    private int step;

    public boolean isLiving() {
        return living;
    }

    public void setLiving(boolean living) {
        this.living = living;
    }

    private boolean living;

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Tank() {
        id = UUID.randomUUID();
    }

    public Tank(int x, int y, Dir dir, boolean living, Group group, TankFrame frame) {
        this();
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.living = living;
        this.group = group;
        this.frame = frame;
        rectangle = new Rectangle(x, y, WIDTH, HEIGHT);
//        TankFrame.INSTANCE.tankMap.put(id, this);
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public void paint(Graphics g) {
        if (!living) {
            frame.tanks.remove(this);
            return;
        }
        if (group == Group.GOOD) {
            mytankPaint(g);
        } else {
            badtankPaint(g);
            if (random.nextInt(10) > 8) fire();
            if (random.nextInt(100) > 95) randomDir();
        }
        move();
        boundCheck();
        rectangle.x = x;
        rectangle.y = y;
    }

    private void boundCheck() {
        if (x < 2) x = 2;
        if (y < 34) y = 34;
        if (x > TankFrame.GAME_WIDTH - WIDTH - 2) x = TankFrame.GAME_WIDTH - WIDTH - 2;
        if (y > TankFrame.GAME_HEIGHT - HEIGHT - 2) y = TankFrame.GAME_HEIGHT - HEIGHT - 2;
    }

    private void randomDir() {
        dir = Dir.values()[random.nextInt(4)];
    }

    private void mytankPaint(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.yellow);
        g.drawString(id.toString(), x, y + 10);
        g.setColor(color);
        switch (dir) {
            case UP:
                g.drawImage(ResourceMgr.tankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.tankD, x, y, null);
                break;
            case LEFT:
                g.drawImage(ResourceMgr.tankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.tankR, x, y, null);
                break;
        }
    }

    private void badtankPaint(Graphics g) {
        switch (dir) {
            case UP:
                g.drawImage(ResourceMgr.badTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.badTankD, x, y, null);
                break;
            case LEFT:
                g.drawImage(ResourceMgr.badTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.badTankR, x, y, null);
                break;
        }
    }

    private void move() {
        int i = step > 1000 ? step = 0 : step++;
        if (!moving) return;
        if (dir == Dir.UP)
            y -= SPEED;
        if (dir == Dir.DOWN)
            y += SPEED;
        if (dir == Dir.LEFT)
            x -= SPEED;
        if (dir == Dir.RIGHT)
            x += SPEED;
        if (group == Group.GOOD) {
            if (i % 3 == 1)
                ResourceMgr.pool.execute(new Audio(ResourceMgr.bytesMove));

        }
    }

    public void fire() {
        if (group == Group.GOOD) ResourceMgr.pool.execute(new Audio(ResourceMgr.bytesFire));
        int bx = x + WIDTH / 2 - Bullet.WIDTH / 2;
        int by = y + HEIGHT / 2 - Bullet.HEIGHT / 2;
        frame.bullets.add(new Bullet(bx, by, dir, true, group, this.frame));

    }

    public void die() {
        setLiving(false);
    }
}
